package com.ys.erp.entity.data.bean;

import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.annotation.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import org.apache.commons.lang3.ObjectUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.*;

/**
 * @author lwj
 * @date 2025/5/16
 */
    
@Data
@TableName(value = "t_user_cat")
public class UserCat {
    /**
     * 主键
     */
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    /**
     * 用户id
     */
    @TableField(value = "user_id")
    private Long userId;

    /**
     * 猫咪id
     */
    @TableField(value = "cat_id")
    private Long catId;

    /**
     * 猫咪编号
     */
    @TableField(value = "cat_code")
    private String catCode;

    /**
     * 猫咪照片
     */
    @TableField(value = "cat_photo")
    private String catPhoto;

    /**
     * 门店code
     */
    @TableField(value = "shop_code")
    private String shopCode;

    /**
     * 领走店铺
     */
    @TableField(value = "shop_name")
    private String shopName;

    /**
     * 用户设置猫咪昵称
     */
    @TableField(value = "user_cat_nick_name")
    private String userCatNickName;

    /**
     * 用户设置猫咪照片
     */
    @TableField(value = "user_cat_photo")
    private String userCatPhoto;

    /**
     * 猫咪昵称
     */
    @TableField(value = "cat_nick_name")
    private String catNickName;

    /**
     * 0套餐领走，1市场价购买
     */
    @TableField(value = "contract_type")
    private Integer contractType;

    /**
     * 领养方式
     */
    @TableField(value = "maintain_method")
    private String maintainMethod;

    /**
     * 用户合约情况
     */
    @TableField(value = "user_fee_remark")
    private String userFeeRemark;

    /**
     * 履约手机号
     */
    @TableField(value = "maintain_phone")
    private String maintainPhone;

    /**
     * 养护服务费
     */
    @TableField(value = "conserve_service_fee")
    private Long conserveServiceFee;

    /**
     * 全款金额
     */
    @TableField(value = "full_money")
    private Long fullMoney;

    /**
     * 猫咪积分余额，单位分
     */
    @TableField(value = "cat_money")
    private Long catMoney;

    /**
     * 是否禁用
     */
    @TableField(value = "is_disable")
    private Integer isDisable;

    /**
     * 是否冻结
     */
    @TableField(value = "is_freeze")
    private Integer isFreeze;

    /**
     * 是否锁定
     */
    @TableField(value = "is_lock")
    private Integer isLock;

    /**
     * 猫咪钱包状态，0预定，1已接走，2送回治疗，3：退定 4死亡
     */
    @TableField(value = "cat_status")
    private Integer catStatus;

    /**
     * 是否员工领养，0否1是
     */
    @TableField(value = "is_emp_take")
    private Integer isEmpTake;

    /**
     * 对接人
     */
    @TableField(value = "channel_emp_ids")
    private String channelEmpIds;

    /**
     * 渠道来源
     */
    @TableField(value = "channel_source")
    private String channelSource;

    /**
     * 领走日期
     */
    @TableField(value = "take_date")
    private LocalDateTime takeDate;

    @TableField(value = "period_no")
    private String periodNo;

    @TableField(value = "period_type")
    private String periodType;

    @TableField(value = "version")
    @Version
    private Integer version;

    @TableField(value = "create_time")
    private LocalDateTime createTime;

    @TableField(value = "update_time", update = "now()")
    private LocalDateTime updateTime;

    @AllArgsConstructor
    @Getter
    public enum Enums {

        BUY,
        PERIOD,
        BUY_IN,
        BUY_PERIOD,

        HUA_BEI,
        WFB,
        MHXX,
        CAF,
        ;

    }

    public static final Map<String, String> ENUM_MAP = new HashMap<>();
    public static final Map<String, Integer> OLD_ENUM_MAP = new HashMap<>();

    public List<String> getPhotoList() {
        if (ObjectUtils.isEmpty(this.getCatPhoto())) {
            return new ArrayList<>();
        }
        return JSONArray.parseArray(this.getCatPhoto(), String.class);
    }

    static {
        ENUM_MAP.put(UserCat.Enums.BUY.name(), "市场价直购");
        ENUM_MAP.put(UserCat.Enums.PERIOD.name(), "无忧养宠");
        ENUM_MAP.put(UserCat.Enums.BUY_IN.name(), "全款充值");
        ENUM_MAP.put(UserCat.Enums.BUY_PERIOD.name(), "小偿无忧");

        OLD_ENUM_MAP.put(UserCat.Enums.BUY.name(), 1);
        OLD_ENUM_MAP.put(UserCat.Enums.PERIOD.name(), 0);
        OLD_ENUM_MAP.put(UserCat.Enums.BUY_IN.name(), 2);
        OLD_ENUM_MAP.put(UserCat.Enums.BUY_PERIOD.name(), 3);

        ENUM_MAP.put(UserCat.Enums.HUA_BEI.name(), "花呗");
        ENUM_MAP.put(UserCat.Enums.WFB.name(), "微分呗");
        ENUM_MAP.put(UserCat.Enums.MHXX.name(), "梅花先享");
        ENUM_MAP.put(UserCat.Enums.CAF.name(), "宠爱付");

    }

    public Integer getAdoptDay() {
        LocalDate now = LocalDate.now();
        LocalDate takeDateLocalDate;
        if (Objects.isNull(this.takeDate)) {
            takeDateLocalDate = Objects.isNull(this.createTime) ? LocalDate.now() : this.createTime.toLocalDate();
        } else {
            takeDateLocalDate = this.takeDate.toLocalDate();
        }
        long daysBetween = ChronoUnit.DAYS.between(takeDateLocalDate, now);
        return (int) daysBetween;
    }

    public String getPeriodTitleDes() {
        if (ObjectUtils.isEmpty(this.getPeriodType())) {
            return "牛油果签约订单：" + this.getCatCode();
        }
        if (Enums.WFB.name().equals(this.getPeriodType())) {
            return "牛油果签约订单：" + this.getCatCode();
        }
        if (Enums.HUA_BEI.name().equals(this.getPeriodType())) {
            return "花呗签约订单：" + this.getCatCode();
        }
        return "";
    }
}